package com.hk.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.hk.model.User;
import com.hk.model.vo.Result;
import com.hk.service.IUserService;
import com.hk.service.impl.UserServiceImpl;
import com.hk.utils.CharacterEncoding;
import com.hk.utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: zhe
 * @Date: 2021/6/10 12:08
 * @Statement: 模糊查询及修改会员状态
 */
@WebServlet("/user/FindByNameOrUpdateState")
public class FindByNameOrUpdateStateServlet extends HttpServlet {

    IUserService iUserService = new UserServiceImpl();

    /**
     * 修改会员状态
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        String id = Tool.nullToString(req.getParameter("id"));
        String state = Tool.nullToString(req.getParameter("state"));
        int intId = Integer.parseInt(id);
        int intState = Integer.parseInt(state);
//        System.out.println("id="+id+"\t"+state);
        User user = new User();
        user.setId(intId);
        user.setState(intState);
        int updateState = iUserService.updateState(user);
        PrintWriter out = resp.getWriter();
        Result result = new Result();
        if (updateState != 1) {
            result.setStatus(updateState);
            result.setMessage("error");
        } else {
            result.setStatus(updateState);
            result.setMessage("success");
        }
        String resultJSON = JSONObject.toJSONString(result);
        out.println(resultJSON);
        out.flush();
        out.close();
    }

}
